package com.badlogic.androidgames.mrnom;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class ActiveFirst extends Activity {
    private final static String NOTES = "notes.txt";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // kiem soat Active
        // true --> da kich hoat
        // false --> chua kich hoat
        boolean boolActive = true;
        // Chuyen sang Activity khac
        Intent intentCallActivity = null;
        // doc du lieu tu file

        if (readFile() == true) {// co du lieu trong file --> da duoc active
            boolActive = false;
        }

        // kiem tra duoc active chua
        if (boolActive == true) {
            // chua duoc Active, yeu cau hien thi Active
            intentCallActivity = new Intent(ActiveFirst.this, ActiveSnake.class);

        } else {
            // Duoc Active, duoc choi game
            intentCallActivity = new Intent(ActiveFirst.this, MrNomGame.class);

        }
        // thuc thi goi Activity
        startActivity(intentCallActivity);
    }

    public boolean readFile() {
        try {
            InputStream in = openFileInput(NOTES);

            if (in != null) {
                InputStreamReader tmp = new InputStreamReader(in);
                BufferedReader reader = new BufferedReader(tmp);
                String str;
                StringBuffer buf = new StringBuffer();

                while ((str = reader.readLine()) != null) {
                    buf.append(str + "\n");
                    return true;// co du lieu trong file
                }

                in.close();
            }
        } catch (java.io.FileNotFoundException e) {
            // that's OK, we probably haven't created it yet
            return false;
        } catch (IOException e) {
            // TODO: handle exception
            return false;
        }
        return false;
    }

}